"""
# -*- coding: utf-8 -*-

Created on 2021/5/28 15:16

@File: create_a_new_station.py
@Author: LT
@Description: Use of this module
"""
from requests import post


class CreateNewStation:
    # TODO: 请求体连接三要素：link，header，params or body
    def __init__(self,
                 station_name: '场站 名称',
                 address: '具体 地址',
                 province: '省 份',
                 city: '市 区',
                 county: '县 域',
                 streettown: '街道 村镇',
                 longitude: '经 度',
                 latitude: '纬 度',
                 manager: '联系 人',
                 tel: '联系 电话',
                 gatewayvender: '网关 厂家',
                 powersupplyarea: '供电 区域',
                 doornumber: '户 号',
                 doorname: '户 名',
                 **kwargs
                 ):
        self.link = 'https://portal-lywz1.eniot.io/configuration/rest/site/addsite'
        self.header = {
            'eos_auth': str({"uid": "35e35736-ce9a-4d1b-b2d5-14ea8c279353",
                             "token": "IAM_s16213859980921",
                             "orgCode": "1859febe5ce70000",
                             "userName": "shujun.wu",
                             "locale": "zh-CN"}).replace('\'', '\"')
        }
        self.params = {
            'sitename': station_name,                                 # 这个应该和siteattr的name.default一致，可以不一致，但得有
            'siteattr': str({"country.default": "中国",
                             "country.zh-CN": "中国",
                             "state.default": province,
                             "state.zh-CN": province,
                             "city.default": city,
                             "city.zh-CN": city,
                             "district.default": county,
                             "district.zh-CN": county,
                             "longitude": longitude,
                             "latitude": latitude,
                             "timezone": "+08:00",
                             "name.default": station_name,
                             "name.zh-CN": station_name}).replace('\'', '\"'),
            'projects':  str([{"name": "工业园区",
                               "deviceList": [],
                               "attributes": [{"key": "name",
                                               "name": "名称",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'true',
                                               "isValueLocalizable": 'true',
                                               "displayId": 1,
                                               "value": station_name},
                                              {"key": "address",
                                               "name": "地址",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 2,
                                               "value": address},
                                              {"key": "province",
                                               "name": "省",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 3,
                                               "value": province},
                                              {"key": "city",
                                               "name": "市",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 4,
                                               "value": city},
                                              {"key": "county",
                                               "name": "县域",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 5,
                                               "value": county},
                                              {"key": "streettown",
                                               "name": "街道村镇",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 6,
                                               "value": streettown},
                                              {"key": "manager",
                                               "name": "负责人",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 7,
                                               "value": manager},
                                              {"key": "tel",
                                               "name": "负责人电话",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 8,
                                               "value": tel},
                                              {"key": "deviceAmount",
                                               "name": "设备总数",
                                               "valueType": 1,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 9},
                                              {"key": "timeZone",
                                               "name": "时区",
                                               "valueType": 5,
                                               "valueTypeArgs": [{"value": "-12: 00",
                                                                  "description": "UTC-12: 00"},
                                                                 {"value": "-11: 00",
                                                                  "description": "UTC-11: 00"},
                                                                 {"value": "-10: 00",
                                                                  "description": "UTC-10: 00"},
                                                                 {"value": "-09: 00",
                                                                  "description": "UTC-09: 00"},
                                                                 {"value": "-08: 00",
                                                                  "description": "UTC-08: 00"},
                                                                 {"value": "-07: 00",
                                                                  "description": "UTC-07: 00"},
                                                                 {"value": "-06: 00",
                                                                  "description": "UTC-06: 00"},
                                                                 {"value": "-05: 00",
                                                                  "description": "UTC-05: 00"},
                                                                 {"value": "-04: 30",
                                                                  "description": "UTC-04: 30"},
                                                                 {"value": "-04: 00",
                                                                  "description": "UTC-04: 00"},
                                                                 {"value": "-03: 30",
                                                                  "description": "UTC-03: 30"},
                                                                 {"value": "-03: 00",
                                                                  "description": "UTC-03: 00"},
                                                                 {"value": "-02: 00",
                                                                  "description": "UTC-02: 00"},
                                                                 {"value": "-01: 00",
                                                                  "description": "UTC-01: 00"},
                                                                 {"value": "Z",
                                                                  "description": "UTC"},
                                                                 {"value": "+01: 00",
                                                                  "description": "UTC+01: 00"},
                                                                 {"value": "+02: 00",
                                                                  "description": "UTC+02: 00"},
                                                                 {"value": "+03: 00",
                                                                  "description": "UTC+03: 00"},
                                                                 {"value": "+03: 30",
                                                                  "description": "UTC+03: 30"},
                                                                 {"value": "+04: 00",
                                                                  "description": "UTC+04: 00"},
                                                                 {"value": "+05: 00",
                                                                  "description": "UTC+05: 00"},
                                                                 {"value": "+05: 30",
                                                                  "description": "UTC+05: 30"},
                                                                 {"value": "+05: 45",
                                                                  "description": "UTC+05: 45"},
                                                                 {"value": "+06: 00",
                                                                  "description": "UTC+06: 00"},
                                                                 {"value": "+06: 30",
                                                                  "description": "UTC+06: 30"},
                                                                 {"value": "+07: 00",
                                                                  "description": "UTC+07: 00"},
                                                                 {"value": "+08: 00",
                                                                  "description": "UTC+08: 00"},
                                                                 {"value": "+09: 00",
                                                                  "description": "UTC+09: 00"},
                                                                 {"value": "+09: 30",
                                                                  "description": "UTC+09: 30"},
                                                                 {"value": "+10: 00",
                                                                  "description": "UTC+10: 00"},
                                                                 {"value": "+11: 00",
                                                                  "description": "UTC+11: 00"},
                                                                 {"value": "+12: 00",
                                                                  "description": "UTC+12: 00"},
                                                                 {"value": "+13: 00",
                                                                  "description": "UTC+13: 00"}],
                                               "isMandatory": 'true',
                                               "isValueLocalizable": 'false',
                                               "displayId": 10,
                                               "value": "-12: 00"},
                                              {"key": "capacity",
                                               "name": "容量(MW)",
                                               "valueType": 2,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 11},
                                              {"key": "projectname",
                                               "name": "项目名称",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 12},
                                              {"key": "projectnumber",
                                               "name": "项目编号",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 13},
                                              {"key": "gatewayvender",
                                               "name": "网关厂家",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 14,
                                               "value": gatewayvender},
                                              {"key": "Y/Nsmallgarden",
                                               "name": "是否为小微园区",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'true',
                                               "isValueLocalizable": 'true',
                                               "displayId": 15,
                                               "value": "否"},
                                              {"key": "subsidiarysmallgarden",
                                               "name": "所属小微园区",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 16,
                                               "value": ""},
                                              {"key": "industry",
                                               "name": "所属行业",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 17,
                                               "value": ""},
                                              {"key": "powersupplyarea",
                                               "name": "供电区域",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 18,
                                               "value": powersupplyarea},
                                              {"key": "doornumber",
                                               "name": "用采系统户号",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 19,
                                               "value": doornumber},
                                              {"key": "doorname",
                                               "name": "用采系统户名",
                                               "valueType": 0,
                                               "valueTypeArgs": [],
                                               "isMandatory": 'false',
                                               "isValueLocalizable": 'false',
                                               "displayId": 20,
                                               "value": doorname}],
                               "deviceTypeList": [{"key": 220,
                                                   "value": "电能表"},
                                                  {"key": 221,
                                                   "value": "变压器温控器"},
                                                  {"key": 222,
                                                   "value": "测试电能表"},
                                                  {"key": 223,
                                                   "value": "变电站"},
                                                  {"key": 224,
                                                   "value": "温湿度探头"},
                                                  {"key": 225,
                                                   "value": "配电柜温度"},
                                                  {"key": 226,
                                                   "value": "水浸"},
                                                  {"key": 227,
                                                   "value": "多功能电表"},
                                                  {"key": 228,
                                                   "value": "烟感"},
                                                  {"key": 229,
                                                   "value": "红外入侵探测"},
                                                  {"key": 230,
                                                   "value": "蒸汽流量计"},
                                                  {"key": 231,
                                                   "value": "冷水机组"},
                                                  {"key": 232,
                                                   "value": "电池储能"},
                                                  {"key": 233,
                                                   "value": "储能电站"},
                                                  {"key": 234,
                                                   "value": "逆变器"},
                                                  {"key": 235,
                                                   "value": "断路器"},
                                                  {"key": 236,
                                                   "value": "水表"},
                                                  {"key": 237,
                                                   "value": "水表"}],
                               "groupTypeList": [],
                               "deviceHeader": ["设备名称",
                                                ["设备模型",
                                                 {},
                                                 "drop_down_header"],
                                                "录入人员",
                                                "操作"],
                               "finalDeviceHeader": ["设备名称",
                                                     ["设备模型",
                                                      {},
                                                      "drop_down_header"],
                                                     "录入人员",
                                                     "操作"],
                               "isNew": 'true',
                               "categoryId": "29",
                               "typeId": 102,
                               "attributeMap": {"name.default": station_name,
                                                "name.zh-CN": station_name,
                                                "address": address,
                                                "province": province,
                                                "city": city,
                                                "county": county,
                                                "streettown": streettown,
                                                "manager": manager,
                                                "tel": tel,
                                                "timeZone": "-12: 00",
                                                "gatewayvender": gatewayvender,
                                                "Y/Nsmallgarden.default": "否",
                                                "Y/Nsmallgarden.zh-CN": "否",
                                                "subsidiarysmallgarden": "",
                                                "industry": "",
                                                "powersupplyarea": powersupplyarea,
                                                "doornumber": doornumber,
                                                "doorname": doorname}}]).replace('\'', '\"')
        }
        self.res = self.__request()

    def __request(self):
        res = post(url=self.link, headers=self.header, params=self.params)
        res.close()
        return res


if __name__ == '__main__':
    exp = CreateNewStation(station_name='待删试验站_create_a_new_station_1',
                           address='1',
                           province='浙江省',
                           city='温州市',
                           county='鹿城区',
                           streettown='江滨街道',
                           longitude='122',
                           latitude='28',
                           manager='TSKJ',
                           tel='88888888',
                           gatewayvender='芜湖',
                           powersupplyarea='江滨供电所',
                           doornumber='666',
                           doorname='777')
    print(exp.res.json())
